﻿using ProjectManager.Logic.Content;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjectManager.UnderLogic
{
    internal static class DataTableToInfo
    {
        public static List<ProjectTaskInformation> ToTaskInfos(this List<DataTable> dataTables)
        {
            Dictionary<int, ProjectTaskInformation> taskDictionary = [];
            Dictionary<int, List<int>> childTaskDictionary = [];
            foreach (DataTable table in dataTables)
            {
                // (ID, TeamID, BranchName, Name, Description, protectedPaths, StartDate, EndDate, Status, ChildTasks)
                ProjectTaskInformation taskInformation = new()
                {
                    ID = table.Rows[0].Field<int>("ID"),
                    TeamID = table.Rows[0].Field<int>("TeamID"),
                    BranchName = table.Rows[0].Field<string>("BranchName") ?? throw new Exception("DataTable: 读取错误"),
                    Path = table.Rows[0].Field<string>("Path") ?? throw new Exception("DataTable: 读取错误"),
                    Name = table.Rows[0].Field<string>("Name") ?? throw new Exception("DataTable: 读取错误"),
                    Description = table.Rows[0].Field<string>("Description") ?? throw new Exception("DataTable: 读取错误"),
                    protectedPaths = table.Rows[0].Field<List<string>>("protectedPaths") ?? throw new Exception("DataTable: 读取错误"),
                    StartDate = table.Rows[0].Field<DateTime>("StartDate"),
                    EndDate = table.Rows[0].Field<DateTime>("EndDate"),
                    Status = table.Rows[0].Field<ProjectTaskStatus>("Status"),
                };
                taskDictionary.Add(taskInformation.ID, taskInformation);
                childTaskDictionary.Add(taskInformation.ID, table.Rows[0].Field<List<int>>("ChildTasks") ?? throw new Exception("DataTable: 读取错误"));
            }
            List<ProjectTaskInformation> result = [];
            foreach (var (key, value) in childTaskDictionary)
            {
                ProjectTaskInformation projectTaskInformation = taskDictionary[key];
                projectTaskInformation.ChildTask = value.Select(x => taskDictionary[x]).ToHashSet();
                result.Add(projectTaskInformation);
            }
            return result;
        }
        public static List<TeamInformation> ToTeamInfos(this List<DataTable> dataTables, List<ProjectTaskInformation> projectTaskInformations)
        {
            Dictionary<int, ProjectTaskInformation> taskDictionary = [];
            foreach (var taskInformation in projectTaskInformations)
            {
                taskDictionary.Add(taskInformation.ID, taskInformation);
            }
            Dictionary<int, TeamInformation> teamDictionary = [];
            Dictionary<int, List<int>> childTeamDictionary = [];
            foreach (DataTable table in dataTables)
            {
                // (ID, Name, Leader, Members, Tasks, IsIndividual, IsCancelled)
                TeamInformation teamInformation = new()
                {
                    ID = table.Rows[0].Field<int>("ID"),
                    Name = table.Rows[0].Field<string>("Name") ?? throw new Exception("DataTable: 读取错误"),
                    Leader = table.Rows[0].Field<MemberInformation>("Leader"),
                    Tasks = table.Rows[0].Field<List<int>>("Tasks")?.Select(x => taskDictionary[x]).ToList() ?? [],
                    IsIndividual = table.Rows[0].Field<bool>("IsIndividual"),
                    IsCancelled = table.Rows[0].Field<bool>("IsCancelled"),
                };
                teamDictionary.Add(teamInformation.ID, teamInformation);
                childTeamDictionary.Add(teamInformation.ID, table.Rows[0].Field<List<int>>("Members") ?? []);
            }
            List<TeamInformation> result = [];
            foreach (var (key, value) in teamDictionary)
            {
                TeamInformation teamInformation = value;
                teamInformation.Members = childTeamDictionary[key].Select(x => teamDictionary[x]).ToHashSet();
                result.Add(teamInformation);
            }
            return result;
        }
    }
}
